package net.zjitc.controller;

import net.zjitc.common.Msg;
import net.zjitc.pojo.Login;
import net.zjitc.pojo.Register;
import net.zjitc.pojo.User;
import net.zjitc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/3/26 11:21
 */
@Controller
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping(value = "/findAll")
    public String findAll(Model model){
        List<User> list = userService.findAll();
        model.addAttribute("list",list);
        return "list.jsp";
    }

    @RequestMapping(value = "/register")
    public String register(Model model, Register register){
        String password = DigestUtils.md5DigestAsHex(register.getPassword().getBytes());
        register.setPassword(password);
        register.setRole("普通用户");
        Date date = new Date();
        register.setRegistTime(date);
        userService.register(register);
        return "/client/registersuccess.jsp";
    }

    @RequestMapping(value = "/checkEmail")
    @ResponseBody
    public Msg checkEmail(@RequestBody User user){
        return userService.checkEmail(user);
    }

    @RequestMapping(value = "/checkUsername")
    @ResponseBody
    public Msg checkUsername(@RequestBody User user){
        return userService.checkUsername(user);
    }

    @RequestMapping(value = "/login")
    public String login(Model model, User user,HttpServletRequest request){
        String password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());

        User fpassword = userService.findByName(user.getUsername());
        System.out.println(fpassword);
        if (fpassword==null){
            model.addAttribute("msg","用户名或密码错误");
            return "/client/login.jsp";
        }

        if (password.equals(fpassword.getPassword())){
            model.addAttribute("suss","登录成功");
            HttpSession session = request.getSession();
            session.setAttribute("user",user);

            return "/index.jsp";
        }
        model.addAttribute("msg","用户名或密码错误（密码）");
        return "/client/login.jsp";
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        return "/index.jsp";
    }


    @RequestMapping(value = "/adminlogin")
    public String adminLogin(Model model, User user, HttpServletRequest request){
        String password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        String username = user.getUsername();
        User fpassword = userService.findByName(username);
        if (fpassword==null){
            model.addAttribute("msg","用户名或密码错误");
            return "/admin/login/login.jsp";
        }

        if (password.equals(fpassword.getPassword())){
            model.addAttribute("suss","登录成功");
            HttpSession session = request.getSession();
            session.setAttribute("user",user);
            return "/admin/login/home.jsp";
        }
        model.addAttribute("msg","用户名或密码错误（密码）");
        return "/admin/login/login.jsp";
    }







    @RequestMapping(value = "/testJson")
    public String testJson(@RequestBody User user){
        System.out.println(user);
        return "/index.jsp";
    }

    @RequestMapping(value = "/testResponseBodyJson")
    @ResponseBody
    public User testResponseBodyJson(@RequestBody User user){
        System.out.println(user);
        User user1 = new User();
        user1.setUsername("adalsdad");
        user1.setPassword("123456789");
        return user1;
    }

}
